package softtech.app.secretcaller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterSecretContacts extends ArrayAdapter<DatabaseContact>{

	private List<DatabaseContact> mListContact;
	private Context mContext;
	private int mode;
	
	public AdapterSecretContacts(Context context, int textViewResourceId , ArrayList<DatabaseContact> list) {
		super(context, textViewResourceId);
		mContext = context;
		mListContact = list;
		mode = 0;
	}

	@Override
	public int getCount() {
		if(mListContact==null || mListContact.equals(null) || mListContact.size() == 0){
			return 1;
		}
		
		return mListContact.size();
	}

	@Override
	public DatabaseContact getItem(int position) {

		if(mListContact==null || mListContact.equals(null) || mListContact.size() == 0){
			return null;
		}
		return mListContact.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if(parent.getContext()==null){		
			return null;
		}
		
		if(mListContact.size()==0){
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.item_search_notfound,parent, false);
			return convertView;
		}
		
		ViewHolder viewHolder=null;
		if(convertView==null){
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.layout_contact_item,parent, false);
			
			viewHolder=new ViewHolder();
			viewHolder.tvContactName	= (TextView) convertView.findViewById(R.id.tvContactName);
			viewHolder.tvContactPhoneNumber = (TextView) convertView.findViewById(R.id.tvContactPhoneNumber);
			viewHolder.imgAvatarContact	= (ImageView) convertView.findViewById(R.id.imgAvatarContact);
			viewHolder.imgCall			= (ImageView) convertView.findViewById(R.id.imgCall);
			
						
			convertView.setTag(viewHolder);
			convertView.setTag(R.id.tvContactName,viewHolder.tvContactName);
			convertView.setTag(R.id.tvContactPhoneNumber,viewHolder.tvContactPhoneNumber);
			convertView.setTag(R.id.imgAvatarContact , viewHolder.imgAvatarContact );
			convertView.setTag(R.id.imgCall, viewHolder.imgCall);
			
		}else{
			viewHolder=(ViewHolder) convertView.getTag();
		}
		
		viewHolder.tvContactName.setText(mListContact.get(position).getContactName());
		viewHolder.tvContactPhoneNumber.setText(mListContact.get(position).getPhoneNumber());
		if(ToolsSaveFile.checkStringNotNull(mListContact.get(position).getAvatar())){
			try{
				File imgFile = new File(mListContact.get(position).getAvatar());
				Bitmap bm = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
				if (bm != null) {
					viewHolder.imgAvatarContact.setImageBitmap(bm);
				} else {
					viewHolder.imgAvatarContact.setImageResource(R.drawable.ic_contact);
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}else{
			viewHolder.imgAvatarContact.setImageResource(R.drawable.ic_contact);
		}
		
		if(mode == 1){
			viewHolder.imgCall.setVisibility(View.GONE);
		}
		
		viewHolder.imgCall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ActivityContactList.mFlagCalling = ActivityContactList.FLAG_CALLING;
				ListenerPhoneState.isInAppDelete = true;
				ListenerPhoneState.phone_number =  mListContact.get(position).getPhoneNumber();
				ControlPhoneAction.makeCall(mContext, mListContact.get(position).getPhoneNumber() );
			}
		});
		
		return convertView;
	}

	static class ViewHolder{
		TextView tvContactName , tvContactPhoneNumber;
		ImageView imgAvatarContact;
		ImageView imgCall;
	}

	public int getMode() {
		return mode;
	}

	public void setMode(int mode) {
		this.mode = mode;
	}
	
}
